﻿#include "message.h"
#include "dbhelper.h"

Message::Message(QObject *parent) : QObject(parent)
{
    relation = nullptr;
    isSend = true;
}

QString Message::getMessageTypeStr(){
    QString messageTypeStr;
    switch (type) {
    case SaleOut:
        messageTypeStr="销售单";
        break;
    case SaleBack:
        messageTypeStr="销退单";
        break;
    case PurchaseIn:
        messageTypeStr="采购单";
        break;
    case PurchaseBack:
        messageTypeStr="采退单";
        break;
    case PurchaseDingdan:
        messageTypeStr="采购订单";
        break;
    case TicketOut:
        messageTypeStr="开票";
        break;
    case MoneyIn:
        messageTypeStr="收款";
        break;
    case TicketIn:
        messageTypeStr="收票";
        break;
    case MoneyOut:
        messageTypeStr="付款";
        break;
    case TicketAdjust:
        messageTypeStr="票款调整";
        break;
    case ProductBatch:
        messageTypeStr="商品批导入";
        break;
    case TicketCount:
        messageTypeStr="发票盘点";
        break;
    case MoneyCount:
        messageTypeStr="资金盘点";
        break;
    case ProductCount:
        messageTypeStr="商品盘点";
        break;
    case SplitMoney:
        messageTypeStr="拆分差价";
        break;
    case FinanceIn:
        messageTypeStr="营外收入";
        break;
    case FinanceOut:
        messageTypeStr="营外支出";
        break;
    case TaxRateChange:
        messageTypeStr="税率变更";
        break;
    }

    return messageTypeStr;
}

double Message::getMoneyValue(){
    switch (type) {
    case SaleOut:
    case SaleBack:
    case PurchaseIn:
    case PurchaseBack:
    case PurchaseDingdan:
        return 0;
    case TicketOut:
        return ticketOut;
    case MoneyIn:
        return moneyIn;
    case TicketIn:
        return ticketIn;
    case MoneyOut:
        return moneyOut;
    case TicketAdjust:
        if(ticketIn != 0.0){
            return ticketIn;
        }
        if(ticketOut != 0.0){
            return ticketOut;
        }
        if(moneyIn != 0.0){
            return moneyIn;
        }
        if(moneyOut != 0.0){
            return moneyOut;
        }
        return 0.0;
    case ProductBatch:
        return ticketIn;
    case TicketCount:
        return ticketIn;
    case MoneyCount:
        return ticketIn;
    case ProductCount:
        return ticketIn;
    case SplitMoney:
        return ticketIn;
    case FinanceIn:
        return ticketIn;
    case FinanceOut:
        return ticketIn;
    case TaxRateChange:
        return ticketIn;
    }

    return 0;
}

void Message::loadFromJsonObject(QJsonObject obj){
    id = obj.value("id").toInt();
    messageId = obj.value("message_id").toString();
    dingdanId = obj.value("dingdan_id").toString();
    sourceUsername = obj.value("source_username").toString();
    targetUsername = obj.value("target_username").toString();
    relationType = obj.value("relation_type").toInt();
    type = obj.value("type").toInt();
    ticketIn = obj.value("ticket_in").toDouble();
    ticketOut = obj.value("ticket_out").toDouble();
    moneyIn = obj.value("money_in").toDouble();
    moneyOut = obj.value("money_out").toDouble();
    date = obj.value("date").toString();
    opNickName = obj.value("op_nick_name").toString();
    remark = obj.value("remark").toString();
}

UserRelation* Message::getUserRelation(){
    if(relation == nullptr){
        DbHelper helper;
        relation = helper.getUserRelation(sourceUsername, targetUsername, relationType==1?UserRelation::Client:UserRelation::Giver);
        if(relation == nullptr){
            //逆向关系
            relation = helper.getUserRelation(targetUsername, sourceUsername, relationType==1?UserRelation::Giver:UserRelation::Client);
        }
    }

    return relation;
}
